
// 在页面或组件中导入
import validate from 'wechat-validate'
const app = getApp()
Page({
  // 通过 behaviors 注入 validate 方法
  behaviors: [validate],
  data: {
    countDownVisible: false,
    mobile:'',
    code:''
  },
  rules: {
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查手机号码是否正确!' },
    ],
    code: [
      { required: true, message: '请填写短信验证码!' },
      { pattern: /^\d{6}$/, message: '请检查短信验证码是否正确!' }
    ]
  },
  countDownChange(ev) {
    // console.log(ev);
    this.setData({
      timeData: ev.detail,
      countDownVisible: ev.detail.minutes === 1 || ev.detail.seconds > 0,
    })
  },
  async getSMSCode(){
    //校验表单
    const {valid,message} = this.validate('mobile')
    // console.log(res,'这是什么鬼东西');
    if(!valid) return wx.utils.toast(message)
    // console.log(1);
    this.setData({
      countDownVisible:true
    })
    const {code} = await wx.http.get('/code',{mobile:this.data.mobile})
    // console.log(code,'codecodecode');
    if(code !== 10000) return wx.utils.toast('发送失败，稍后重试!')
    // console.log(data);
  },
  onLoad({redirectURL}){
  // console.log(redirectURL);
  this.redirectURL = redirectURL
  },
  //点击登录
  async submitForm(){
  //  console.log(1);
  if(!this.validate()) return
  //  this.validate()
  //  console.log(1);
  const {code,data} = await wx.http.post('/login',{
    mobile:this.data.mobile,
    code:this.data.code
  })
  if(code!==10000) return wx.utils.toast()
  app.setToken('token',data.token)
  app.setToken('refreshToken',data.refreshToken)
  wx.redirectTo({
    url:this.redirectURL || '/pages/index/index',
  })
  }
})
